package com.ruoyi.web.controller.vidiomonitor;
/**
 * ClassName: VideoAbilityController <br/>
 * Description: <br/>
 * date: 2021/1/4 10:43<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.Filebyte;
import com.ruoyi.common.utils.hikvisionutils.model.HIKResponseResult;
import com.ruoyi.common.utils.hikvisionutils.model.HikVisionAbilityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Map;

/**
 * @program: ruoyi
 * @description: 视频能力
 * @author: hesion
 * @create: 2021-01-04 10:43
 **/
@Api("视频应用-视频能力")
@RestController
@RequestMapping("/videoapi/ability")
public class VideoAppAbilityController {
    @ApiOperation("获取监控点预览取流URL")
    @RequestMapping(value = "/previewURLs",method = RequestMethod.POST)
    public AjaxResult getPreviewURLs(@RequestBody Map<String, Object> params) throws InterruptedException {
        String body = HikVisionAbilityUtils.getPreviewURLs(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("获取监控点回放取流URL")
    @RequestMapping(value = "/playbackURLs",method = RequestMethod.POST)
    public AjaxResult getPlaybackURLs(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.getPlaybackURLs(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("查询对讲URL")
    @RequestMapping(value = "/talkURLs",method = RequestMethod.POST)
    public AjaxResult getTalkURLs(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.getTalkURLs(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("根据监控点编号进行云台操作")
    @RequestMapping(value = "/controlling",method = RequestMethod.POST)
    public AjaxResult getControlling(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.getControlling(params).getBody();
        Map result = JSONObject.parseObject(body);
        return AjaxResult.success(result);
    }
    @ApiOperation("手动抓图")
    @RequestMapping(value = "/manualCapture",method = RequestMethod.POST)
    public void doManualCapture(@RequestBody Map<String, Object> params, final HttpServletResponse response, final HttpServletRequest request) throws Exception {
        OutputStream out;
        BufferedOutputStream bos ;
        BufferedInputStream bis = null;
        String body = HikVisionAbilityUtils.doManualCapture(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        URL url = new URL((String) hikResponseResult.getData().get("picUrl"));
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5 * 1000);
        response.reset();
        response.setContentType("application/x-download;charset=GBK");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String("1.jpg".getBytes("utf-8"), "iso-8859-1"));
        InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
        byte[] btImg = readInputStream(inStream);//得到图片的二进制数据
        out = response.getOutputStream();
        bos = new BufferedOutputStream(out);
        bos.write(btImg);
        bos.flush();
        try {
            if (bos != null) {
                bos.close();
            }
        } catch (IOException e) {

        }
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {

        }
        try {
            if (bis != null) {
                bis.close();
            }
        } catch (IOException e) {

        }
    }
    @ApiOperation("监控点3D放大")
    @RequestMapping(value = "/selZoom",method = RequestMethod.POST)
    public AjaxResult doSelZoom(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.doSelZoom(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("开始手动录像")
    @RequestMapping(value = "/manualRecord/start",method = RequestMethod.POST)
    public AjaxResult doManualRecordStart(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.doManualRecordStart(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("停止手动录像")
    @RequestMapping(value = "/manualRecord/stop",method = RequestMethod.POST)
    public AjaxResult doManualRecordStop(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.doManualRecordStop(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("获取手动录像状态")
    @RequestMapping(value = "/manualRecord/status",method = RequestMethod.POST)
    public AjaxResult doManualRecordStatus(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.doManualRecordStatus(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("设置预置点信息")
    @RequestMapping(value = "/presets/addition",method = RequestMethod.POST)
    public AjaxResult setPresetsAddition(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.setPresetsAddition(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("查询预置点信息")
    @RequestMapping(value = "/presets/searches",method = RequestMethod.POST)
    public AjaxResult getPresetsSearches(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.getPresetsSearches(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("删除预置点信息")
    @RequestMapping(value = "/presets/deletion",method = RequestMethod.POST)
    public AjaxResult delPresetsDeletion(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.delPresetsDeletion(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("批量获取监控点的预置点信息")
    @RequestMapping(value = "/presets/get",method = RequestMethod.POST)
    public AjaxResult getPresets(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.getPresets(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    @ApiOperation("获取视频事件的图片")
    @RequestMapping(value = "/events/picture",method = RequestMethod.POST)
    public AjaxResult getEventsPicture(@RequestBody Map<String, Object> params) {
        String body = HikVisionAbilityUtils.getEventsPicture(params).getBody();
        HIKResponseResult hikResponseResult = JSONObject.parseObject(body, HIKResponseResult.class);
        return AjaxResult.success(hikResponseResult.getData());
    }
    /**
     * 从输入流中获取数据
     * @param inStream 输入流
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }


}
